package player;

import java.util.ArrayList;

/**
 *
 * @author Michael Hanns
 *
 */
public class LogBookImp implements LogBook, LogBookEdit {

    private ArrayList<BookEntry> entries;
    private ArrayList<BookEntry> completedEntries;

    public LogBookImp() {
        entries = new ArrayList<BookEntry>();
        completedEntries = new ArrayList<BookEntry>();
    }

    @Override
    public BookEntry[] getAllEntries() {
        BookEntry[] entriesOut = new BookEntry[entries.size() + completedEntries.size()];

        for(int x = 0; x < entries.size(); x++) {
            entriesOut[x] = entries.get(x);
        }

        for(int x = 0; x < completedEntries.size(); x++) {
            entriesOut[x+entries.size()] = completedEntries.get(x);
        }

        return entriesOut;
    }

    @Override
    public void addEntry(BookEntry e) {
        if(!e.isComplete()) {
            entries.add(e);
        } else {
            completedEntries.add(e);
        }
    }

    @Override
    public void completeEntry(int id) {
        for (int x = 0; x < entries.size(); x++) {
            if (entries.get(x).getID() == id) {
                BookEntry e = entries.get(x);
                entries.remove(e);
                e.setComplete(true);
                completedEntries.add(e);
            }
        }
    }

    @Override
    public boolean entryExists(int id) {
        for (int x = 0; x < entries.size(); x++) {
            if (entries.get(x).getID() == id) {
                return true;
            }
        }
        for (int x = 0; x < completedEntries.size(); x++) {
            if (completedEntries.get(x).getID() == id) {
                return true;
            }
        }
        return false;
    }
    
    private BookEntry getEntry(int id) {
        for (int x = 0; x < entries.size(); x++) {
            if (entries.get(x).getID() == id) {
                return entries.get(x);
            }
        }
        for (int x = 0; x < completedEntries.size(); x++) {
            if (completedEntries.get(x).getID() == id) {
                return completedEntries.get(x);
            }
        }
        return null;
    }

    @Override
    public boolean entryIncomplete(int id) {
        for (int x = 0; x < entries.size(); x++) {
            if (entries.get(x).getID() == id) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean entryCompleted(int id) {
        for (int x = 0; x < completedEntries.size(); x++) {
            if (completedEntries.get(x).getID() == id) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String viewEntry(int id) {
        BookEntry e = getEntry(id);
        if (e != null) {
            return e.getEntry();
        } else {
            return "That log entry does not exist!";
        }
    }

    @Override
    public String viewLog(int page) {
        String output = "You have " + entries.size() + " unresolved entries in your log. "
                + "Use READENTRY <number> to read more details.\n\n";

        if (page == 0 || entries.size() == 0) {
            return output + "Use READLOG to view entries, or READCOMPLETE "
                    + "to read completed entries.";
        } else if (entries.size() > page * 10 - 10) {
            for (int x = (page * 10 - 10); x < entries.size() && x < page * 10; x++) {
                output = output.concat(String.valueOf(entries.get(x).getID()).concat(
                		". ".concat(entries.get(x).getTitle() + "\n")));
            }

            output = output.concat("\nPage ".concat(String.valueOf(page).concat(" of ".concat(
            		String.valueOf((entries.size() / 10 + 1)).concat(
                    ". Use READLOG <page> to view more entries, or READCOMPLETE ".concat(
                    "<page> to read completed entries."))))));
        } else {
            return "That log page does not exist!";
        }

        return output;
    }

    @Override
    public String viewCompleteLog(int page) {
        String output = "You have " + completedEntries.size() + " complete entries in your log. "
                + "Use READENTRY <id> to read more details.\n\n";

        if (page == 0 || completedEntries.size() == 0) {
            return output;
        } else if (completedEntries.size() > page * 10 - 10) {
            for (int x = (page * 10 - 10); x < completedEntries.size() && x < page * 10; x++) {
                output = output.concat(String.valueOf(completedEntries.get(x).getID()).concat(
                		". ".concat(completedEntries.get(x).getTitle().concat("\n"))));
            }

            output = output.concat("\nPage ".concat(String.valueOf(page).concat(" of ".concat(
            		String.valueOf(completedEntries.size() / 10 + 1).concat(
                    ". Use READCOMPLETE <page> to view more entries.")))));
        } else {
            return "That log page does not exist!";
        }

        return output;
    }
}
